package lpl.com.xyd.test.manage.impl;

import com.alibaba.fastjson.JSONObject;
import lpl.com.xyd.test.manage.common.response.HttpResponse;
import lpl.com.xyd.test.manage.convert.ProjectDemandsConvert;
import lpl.com.xyd.test.manage.dao.ProjectMapperDAO;
import lpl.com.xyd.test.manage.domain.ProjectDemandsDomain;
import lpl.com.xyd.test.manage.facade.ProjectDemandsFacade;
import lpl.com.xyd.test.manage.factory.ProjectDemandsDomainFactory;
import lpl.com.xyd.test.manage.factory.ProjectDomainFactory;
import lpl.com.xyd.test.manage.info.ProjectDemandsInfo;
import lpl.com.xyd.test.manage.order.ProjectDemandsAddOrder;
import lpl.com.xyd.test.manage.order.ProjectDemandsQueryOrder;
import lpl.com.xyd.test.manage.param.ProjectDemandsParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectDemandsFacadeImpl implements ProjectDemandsFacade {

    private final static Logger LOGGER = LoggerFactory.getLogger(ProjectFacadeImpl.class);

    @Autowired
    private ProjectDemandsDomainFactory projectDemandsDomainFactory;
    @Autowired
    private ProjectDomainFactory projectDomainFactory;

    @Autowired
    private ProjectMapperDAO projectMapperDAO;

    @Override
    public HttpResponse<ProjectDemandsInfo> addProjectDemands(ProjectDemandsAddOrder order) {
        LOGGER.info("新增项目需求，order:{}", JSONObject.toJSONString(order));
        HttpResponse<ProjectDemandsInfo> response = new HttpResponse<>();
        ProjectDemandsParam param = new ProjectDemandsParam();
        BeanUtils.copyProperties(order,param);
        ProjectDemandsDomain domain = projectDemandsDomainFactory.create(param);
        Boolean result = projectDemandsDomainFactory.store(domain);
        if (!result){
            response.failfure();
            response.setMessage("系统异常，稍后再试");
        }
        response.successful();
        response.setMessage("保存成功");
        return response;
    }

    @Override
    public HttpResponse<List<ProjectDemandsInfo>> query(ProjectDemandsQueryOrder order) {
        LOGGER.info("查询项目需求，order:{}", JSONObject.toJSONString(order));
        HttpResponse<List<ProjectDemandsInfo>> response = new HttpResponse<>();
        ProjectDemandsParam param = new ProjectDemandsParam();
        BeanUtils.copyProperties(order,param);
        Long projectId = projectMapperDAO.selectByName(order.getProjectName());
        param.setProjectId(projectId);
        List<ProjectDemandsDomain> domains = projectDemandsDomainFactory.activeList(param);
        if (null == domains || domains.size() <= 0){
            response.failfure();
            response.setMessage("没有数据");
            return response;
        }
        List<ProjectDemandsInfo> infos = new ArrayList<>(domains.size());
        domains.forEach(v->{
            ProjectDemandsInfo info = ProjectDemandsConvert.convert(v);
            infos.add(info);
        });
        response.successful();
        response.setData(infos);
        return response;
    }

    @Override
    public HttpResponse<ProjectDemandsInfo> update(ProjectDemandsQueryOrder order) {
        return null;
    }
}
